package com.cll.controller.user;

import com.cll.dto.ShoppingCartDTO;
import com.cll.entity.ShoppingCart;
import com.cll.result.Result;
import com.cll.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "购物车相关接口")
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车", shoppingCartDTO);

        //调用业务层添加购物车数据
        shoppingCartService.add(shoppingCartDTO);

        return Result.success();

    }

    /**
     * 查询获取购物车里所有集合
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询获取购物车里所有集合")
    public Result<List<ShoppingCart>> list(){
        log.info("查询获取购物车里面的集合");
        return Result.success(shoppingCartService.list());
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result clean(){
        log.info("清空购物车");
        shoppingCartService.clean();
        return Result.success();
    }


    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("删除购物车中一个商品")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车中一个商品", shoppingCartDTO);

        //调用业务层添加购物车数据
        shoppingCartService.sub(shoppingCartDTO);

        return Result.success();

    }

}
